﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Script;
using P.InOut.NetMvc.Common;
using P.InOut.NetMvc.Models;
using P.InOut.NetMvc.Properties;

namespace P.InOut.NetMvc.Controllers
{
    public class UpdateComponentController : ComponentController
    {
        /// <summary>
        /// Returns Updates for all Team-Mates in the teams that you are a member of
        /// </summary>
        public void Updates()
        {
            // Get User
            MembershipUser user = Membership.GetUser(this.Context.HttpContext.User.Identity.Name);

            // Open and Dispose of Data Context
            using (InOutDataContext db = new InOutDataContext(Settings.Default.dbConn))
            {
                // Get list of teams user is a member of
                List<Team> memberOf = (from t in db.MemberTeams
                                       where t.UserId == (Guid)user.ProviderUserKey
                                       select t.Team).ToList<Team>();

                // Get list of updates for members in the same teams as user
                List<Update> updates = (from u in db.Updates
                                        orderby u.DateTimeEntered descending
                                        where u.Aspnet_Membership.MemberTeams.Any(m => memberOf.Contains(m.Team))
                                            && u.Active == true
                                        select u).Take(35).ToList();

                // Set the ViewData equal to the Updates queried
                var viewData = new UpdateData
                {
                    Updates = updates
                };

                // Render the View
                RenderView("Updates", viewData);
            }
        }

        /// <summary>
        /// Returns User and Update data for each Teammate in the teams you are a member of
        /// </summary>
        public void TeamUpdates()
        {
            // Get User
            MembershipUser user = Membership.GetUser(this.Context.HttpContext.User.Identity.Name);

            // Open and Dispose of Data Context
            using (InOutDataContext db = new InOutDataContext(Settings.Default.dbConn))
            {
                // Get list of teams user is a member of
                List<Team> memberOf = (from t in db.MemberTeams
                                       where t.UserId == (Guid)user.ProviderUserKey
                                       select t.Team).ToList<Team>();

                // Get a list of Teammates in the groups user is also a member of
                List<Aspnet_Membership> teammates = (from t in db.Aspnet_Memberships
                                                     where t.MemberTeams.Any(m => memberOf.Contains(m.Team) && 
                                                         m.UserId != (Guid)user.ProviderUserKey)
                                                     select t).ToList();

                // Create what will be the ViewData and insert record for user
                List<MemberUpdates> memberUpdates = new List<MemberUpdates>();
                memberUpdates.Add(new
                    MemberUpdates
                    {
                        User = db.Aspnet_Memberships.Where(m => m.UserId == (Guid)user.ProviderUserKey).SingleOrDefault(),
                        
                        Updates = (from u in db.Updates
                                   orderby u.DateTimeEntered descending
                                   where u.UserId == (Guid)user.ProviderUserKey
                                        && u.Active == true
                                   select u).Take(3).ToList<Update>()
                    });

                // For each teammate, add a record
                foreach (Aspnet_Membership member in teammates)
                {
                    memberUpdates.Add(new 
                        MemberUpdates
                        {
                            User = member,
                            Updates = (from u in db.Updates
                                       orderby u.DateTimeEntered descending
                                       where u.UserId == member.UserId
                                            && u.Active == true
                                       select u).Take(3).ToList<Update>()
                        });
                }
                
                // Set the ViewData equal to the queried data
                var viewData = new TeamUpdateData 
                {
                    MemberUpdates = memberUpdates
                };

                // Render the View
                RenderView("TeamUpdates", viewData);
            }
        }

        public void UserUpdates()
        {
            // Get User Id
            string id = this.Context.RouteData.Values["id"].ToString();

            // Get User
            MembershipUser user = Membership.GetUser(id);

            // Open and Dispose of Data Context
            using (InOutDataContext db = new InOutDataContext(Settings.Default.dbConn))
            {
                // Get list of updates for members in the same teams as user
                List<Update> updates = (from u in db.Updates
                                        orderby u.DateTimeEntered descending
                                        where u.UserId.ToString() == id && u.Active == true
                                        select u).ToList();

                // Set the ViewData equal to the Updates queried
                var viewData = new UpdateData
                {
                    Updates = updates
                };

                // Render the View
                RenderView("UserUpdates", viewData);
            }
        }
    }

    public class UpdateData
    {
        /// <summary>
        /// List of all Updates sorted by DateTime Stamp
        /// </summary>
        public List<Update> Updates { get; set; }
    }

    public class TeamUpdateData
    {
        /// <summary>
        /// List of Members and their associated Updates
        /// </summary>
        public List<MemberUpdates> MemberUpdates { get; set; }
    }
}
